<template>
  <div class="login">
    <h1>欢迎登录</h1>
    <div class="form">
      <div class="item">
        <span class="iconfont icon-user"></span>
        <input type="text" placeholder="请输入手机号码" v-model="phone" />
      </div>
      <div class="item">
        <span class="iconfont icon-user"></span>
        <input type="password" placeholder="请输入密码" v-model="password" @keyup.enter="getCode">
        <!-- <button class="getCode" @click="getCode">发送</button> -->
      </div>
      <div class="item">
        <!-- <div class="submit" @click="submit">登录</div> -->
        <div class="submit" @click="getCode">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
import {getPassword } from "../../api/base";
// import { getCaptcha, getCaptchaVerify,getPassword } from "../../api/base";
// import {getCaptcha} from "../../api/base"
// import { Notify } from "vant";
import { Toast } from 'vant'
export default {
  data() {
    return {
      phone: "",
      captcha: "",
      password:"",
      id:""
    };
  },
  methods: {
    getCode() {
      let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
      // if (!reg.test(this.phone)) {
      //   Toast({ type: "warning", message: "请输入正确手机号码" });
      //   return;
      // }
      // getCaptcha({ phone: this.phone }).then((data) => {
      //   if (data.code == 200) {
      //     Toast({ type: "success", message: "验证码发送成功" });
      //   }
      // });
      getPassword({phone: this.phone,password:this.password}).then((data)=>{
        console.log(data);
        // console.log(data.account.id);
        // this.id = data.account.id;
        if(!reg.test(this.phone)){
          Toast({ type: "warning", message: "请输入正确手机号码" });
          return;
        }else if(this.password == ''){
          Toast({ type: "fail", message: "请输入密码" });
          return;
        }else if(data.code == 502){
          Toast({ type: "fail", message: "密码错误" });
          return;
        }else if(data.code == 200){
          Toast.success({ type: "success", message: "登录成功" });
          window.localStorage.setItem("login", "true");
          this.$store.commit("setLogin");
          this.$store.commit("saveUserId",data.account.id);
          this.$router.push("/recommend");
        }
      })
    },
    // submit() {
      // this.$store.commit("setLogin");
      // window.localStorage.setItem("login", "true");
    //   this.$router.push("/recommend");
    //   let reg = /^[0-9]{4}$/;
    //   if (!reg.test(this.captcha)) {
    //     Toast({ type: "warning", message: "请输入正确验证码" });
    //     return;
    //   }
    //   getCaptchaVerify({ phone: this.phone, captcha: this.captcha }).then(
    //     (data) => {
    //       if (data.code == 200) {
    //         Toast({ type: "success", message: "登录成功" });
    //         window.localStorage.setItem("login", "true");
    //         this.$store.commit("setLogin");
    //         this.$router.push("/recommend");
    //       } else {
    //         Toast({ type: "warning", message: "登录失败" });
    //       }
    //     }
    //   );
    // },
  },
};
</script>

<style lang="less" scoped>
.login {
  background: #0379db;
  width: 100%;
  height: 100%;
  color: #fff;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
h1 {
  font-size: 48px;
  text-align: center;
  height: 48px;
  padding: 203px 0 85px 0;
}
.form {
  .item {
    width: 76%;
    height: 60px;
    line-height: 60px;
    margin: 0 auto;
    background-color: #00B0F0;
    border-radius: 30px;
    margin-bottom: 24px;
    input {
      background-color: transparent;
      border: 0px;
      outline: none;
      text-indent: 0.5em;
      font-size: 25px;
      color: #fff;
    }
    span {
      font-size: 40px;
      margin-left: 10px;
      display: inline-block;
      vertical-align: middle;
    }
    .getCode {
      border: 1px solid #fff;
      height: 40px;
      line-height: 40px;
      width: 100px;
      text-align: center;
      border-radius: 20px;
      color: #0379db;
      font-size: 20px;
      background-color: #fff;
    }
    .submit {
      font-size: 30px;
      background-color: #fff;
      text-align: center;
      color: #0379db;
      border-radius: 30px;
    }
  }
}
</style>